package com.example.fw;

import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JLabelOperator;

import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 02.04.13
 * Time: 14:59
 * To change this template use File | Settings | File Templates.
 */
public class HelperBase {
    protected final ApplicationManager applicationManager;
    protected final JFrameOperator mainFrame;

    protected String waitMessageDialog(String title, int timeOut) {
        long initialTime = System.currentTimeMillis();
        long currentTime = initialTime;
        int pauseTime = 500;

        while (currentTime < initialTime + timeOut){
            JDialog dialog = JDialogOperator.findJDialog(applicationManager.getApplication().getOwner(), title, false, false);
            if (dialog != null) {
                JDialogOperator dialogOperator = new JDialogOperator(dialog);
                String message = new JLabelOperator(dialogOperator).getText();
                dialogOperator.requestClose();
                return message;
            }
            try {
                Thread.sleep(pauseTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            currentTime+= pauseTime;
        }
        return null;
    }

    public HelperBase(ApplicationManager applicationManager) {
        this.applicationManager = applicationManager;
        this.mainFrame = applicationManager.getApplication();
    }
}
